//
//  EasyPoTests.m
//  xsd2cocoa
//
//  Created by Stefan Winter on 11.09.11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "EasyPoTests.h"
#import "TemporaryDirectoryCategory.h"
#import "XSDschema.h"
#import "POPurchaseOrder.h"

@implementation EasyPoTests

- (void)testTemplateGeneration
{
    NSError* error = nil;
    XSDschema* schema = [XSDschema newWithUrl: schemaUrl prefix: nil error: &error];
    
    // Get path to template.
	NSURL* templatePath = [bundle URLForResource:@"standard-template" withExtension: @"xml"];
    [schema loadTemplate: templatePath error: &error];
    
    NSFileManager* fm = [NSFileManager defaultManager];
    NSURL* tmpOutFolder = [NSURL fileURLWithPath: [fm createTemporaryDirectory] isDirectory: TRUE];
    
    [schema generateInto: tmpOutFolder error: &error];
    NSLog(@"%@", error);
    STAssertNil(error, @"no error should be returned");
    
    NSArray* files = [fm contentsOfDirectoryAtURL: tmpOutFolder includingPropertiesForKeys:nil options: NSDirectoryEnumerationSkipsSubdirectoryDescendants error: &error];
    NSLog(@"Processed template:\r%@", files);
    
    STAssertEquals(8, (int) files.count, @"there must be 8 files");
    
    [fm removeItemAtPath: [tmpOutFolder path] error: &error];
}

- (void) testXmlLoadingWithSpaces {
    NSURL *xml1URL = [bundle URLForResource:@"easypo_example01" withExtension:@"xml"];
    
    NSData* fileData = [NSData dataWithContentsOfURL: xml1URL];
    
    POPurchaseOrder* po1 = [POPurchaseOrder fromPurchaseOrder: fileData];
    
    STAssertTrue(po1 != nil, @"reader must be loaded");
    STAssertNotNil( po1.date, @"date read");
    STAssertEquals(2, (int) [po1.lineItem count], @"line items");
    POLineItem* item1 = [po1.lineItem objectAtIndex: 0];
    STAssertNotNil(item1.price, @"price");
}

- (void)setUp
{
    [super setUp];
    
    bundle = [[NSBundle bundleForClass:[EasyPoTests class]] retain];
    schemaUrl = [[bundle URLForResource:@"easypo" withExtension:@"xsd"] retain];
}

- (void)tearDown
{
    [schemaUrl release];
    [bundle release];
    
    [super tearDown];
}

@end
